﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UpdateServers.Command;
using UpdateServers.Models;

namespace UpdaterClient
{
    /// <summary>
    /// 接连设置
    /// </summary>
    public partial class FormSet : Form
    {
        private FileConfigModels fileConfigModels;
        public FormSet()
        {
            InitializeComponent();
        }

        private void FormSet_Load(object sender, EventArgs e)
        {
            fileConfigModels = FileConfigHelper.GetFileConfig();
            if (fileConfigModels != null)
            {
                this.txt_IpAddr.Text = fileConfigModels.IpAddr;
                this.Num_Protar.Value = Convert.ToDecimal(fileConfigModels.Port);
                this.cb_IsAutoStart.Checked = fileConfigModels.IsAutoStart;
            }
        }

        private void bt_Test_Click(object sender, EventArgs e)
        {
            var ip = this.txt_IpAddr.Text.Trim();
            var prot = this.Num_Protar.Value;
            if (string.IsNullOrEmpty(ip))
            {
                MessageBox.Show("IP不能为空！", "提示");
                return;
            }

            try
            {
                var ipa = IPAddress.Parse(ip);
                var IPAddr = new IPEndPoint(ipa.Address, (int)prot);
                using (var ClientServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    ClientServer.Connect(IPAddr);
                    if (ClientServer.Connected)
                    {
                        MessageBox.Show("连接成功！", "提示");
                    }
                    else
                    {
                        MessageBox.Show("连接失败！", "提示");
                    }
                    ClientServer.Disconnect(false);
                    ClientServer.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("异常：" + ex.Message, "提示");
            }

        }
        /// <summary>
        /// 保存配制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_Save_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            fileConfigModels = new FileConfigModels()
            {
                IpAddr = this.txt_IpAddr.Text.Trim(),
                Port = (int)this.Num_Protar.Value,
                IsAutoStart = this.cb_IsAutoStart.Checked,
            };
            var ash = AutoStartHelper.SetAutoStart(fileConfigModels.IsAutoStart, out error);
            if (ash != true)
            {
                MessageBox.Show(error, "提示");
                return;
            }
            FileConfigHelper.SetFileConfig(fileConfigModels);
            this.Close();
        }

        private void cb_IsAutoStart_CheckedChanged(object sender, EventArgs e)
        {
            var IsAutoStart = this.cb_IsAutoStart.Checked;

        }
    }
}
